<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      table {
        border-collapse: collapse;
        text-align: center;
      }

      thead {
        background-color: #f2f2f2;
      }

      td,
      th {
        padding: 10px 16px;
        border: 1px solid #aaa;
      }
    </style>
  </head>
  <body>
    <div id="root"></div>
    <script src="./lib/react.js"></script>
    <script src="./lib/react_dom.js"></script>
    <script src="./lib/babel.js"></script>

    <script src="./data.js"></script>
    <script src="./format.js"></script>

    <script type="text/babel">
      class App extends React.Component {
        constructor() {
          super()
          this.state = {
            books: books,
          }
        }

        getTotalPrice() {
          const { books } = this.state
          /* let totalPrice = 0
          for (let i = 0; i < books.length; i++) {
            totalPrice += books[i].price * books[i].count
          } */
          const totalPrice = books.reduce((pre, cur) => {
            return pre + cur.price * cur.count
          }, 0)

          return totalPrice
        }

        increment(index) {
          // 浅拷贝, react中不能直接使用原数据
          const newBooks = [...this.state.books]
          newBooks[index].count += 1
          this.setState({
            books: newBooks,
          })
        }

        decrement(index) {
          const newBooks = [...this.state.books]
          newBooks[index].count -= 1
          this.setState({
            books: newBooks,
          })
        }

        // 封装increment，decrement
        changeCount(index, count = 1) {
          const newBooks = [...this.state.books]
          newBooks[index].count += count
          this.setState({
            books: newBooks,
          })
        }

        delBook(index) {
          const newBooks = [...this.state.books]
          // splice: 返回被删除元素组合的数组, 直接回影响原数组
          const res = newBooks.splice(index, 1)
          this.setState({
            books: newBooks,
          })
        }

        renderItem() {
          const { books } = this.state
          return (
            <div>
              <h2>购物车案例</h2>
              <table>
                <thead>
                  <tr>
                    <th>序号</th>
                    <th>书籍名称</th>
                    <th>出版日期</th>
                    <th>价格</th>
                    <th>购买数量</th>
                    <th>操作</th>
                  </tr>
                </thead>
                <tbody>
                  {books.map((book, index) => (
                    <tr key={book.id}>
                      <td>{index + 1}</td>
                      <td>{book.name}</td>
                      <td>{book.date}</td>
                      <td>{formatPrice(book.price)}</td>
                      <td>
                        <button
                          disabled={book.count <= 1}
                          onClick={() => this.changeCount(index, -1)}
                        >
                          -
                        </button>
                        {book.count}
                        <button onClick={() => this.changeCount(index)}>
                          +
                        </button>
                      </td>
                      <td>
                        <button onClick={() => this.delBook(index)}>
                          删除
                        </button>
                      </td>
                    </tr>
                  ))}
                </tbody>
              </table>
              <h2>总价给: {formatPrice(this.getTotalPrice())}</h2>
            </div>
          )
        }

        renderEmpty() {
          return <h2>购物车数据为空</h2>
        }

        render() {
          const { books } = this.state

          return books.length ? this.renderItem() : this.renderEmpty()
        }
      }

      const root = ReactDOM.createRoot(document.querySelector('#root'))
      root.render(<App />)
    </script>
  </body>
</html>
